package com.icetec.iceintel.ui.adapter;

import android.content.Intent;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.ArrayList;

import com.icetec.R;
import com.icetec.iceintel.database.AlarmConfigSqlHelper;
import com.icetec.iceintel.framework.AppApplication;
import com.icetec.iceintel.model.Alarm;
import com.icetec.iceintel.netrequest.InstrutionSender;
import com.icetec.iceintel.service.AlarmServiceBroadcastReciever;
import com.icetec.iceintel.ui.activity.AlarmModificationActivity;
import com.icetec.iceintel.utility.ToastUtility;

/**
 * Created by amo on 2015/2/16.
 * 闹钟列表设配器
 */
public class AlarmListAdapter extends BaseAdapter{

    private Resources resources;
    private ArrayList<Alarm> alarmArrayList;
    LayoutInflater layoutInflater;
    AlarmConfigSqlHelper alarmConfigSqlHelper;
    InstrutionSender instrutionSender;

    public AlarmListAdapter(){

        layoutInflater=LayoutInflater.from(AppApplication.getShareInstance());
        resources = AppApplication.getShareInstance().getResources();
        alarmConfigSqlHelper=new AlarmConfigSqlHelper();
        alarmArrayList=alarmConfigSqlHelper.getAllAlarm(Alarm.AlarmType.AWAKE);
        instrutionSender=new InstrutionSender();

    }


    public void refreshData(){

        alarmArrayList=alarmConfigSqlHelper.getAllAlarm(Alarm.AlarmType.AWAKE);
        notifyDataSetChanged();

    }



    @Override
    public int getCount() {
        return alarmArrayList.size() ;
    }

    @Override
    public Object getItem(int position) {
        return alarmArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView =layoutInflater.inflate(
                    R.layout.component_alarm_list_element, null);

        final Alarm alarm = (Alarm) getItem(position);

        CheckBox checkBox = (CheckBox)  convertView.findViewById(R.id.checkBox_alarm_active);
        checkBox.setChecked(alarm.getAlarmActive());
        checkBox.setTag(position);

  //     checkBox.setOnClickListener(alarmActivity);

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                alarm.setAlarmActive(isChecked);


                if(isChecked){
                    ToastUtility.show(alarm.getTimeUntilNextAlarmMessage());
                    AlarmModificationActivity.allocatePort(alarm);
                    ToastUtility.show(resources.getString(R.string.alarm_is)
                            +alarm.getAlarmPort()+resources.getString(R.string.activated));
                    alarmConfigSqlHelper.update(alarm);

                }

                else{
                    ToastUtility.show(resources.getString(R.string.alarm_is)
                            + alarm.getAlarmPort() + resources.getString(R.string.cancelled));
                    alarmConfigSqlHelper.update(alarm);
                }

                instrutionSender.setAlarm(alarm);



  //              refreshData();
                callMathAlarmScheduleService();
            }
        });


        TextView alarmTimeView = (TextView)  convertView
                .findViewById(R.id.textView_alarm_time);
        alarmTimeView.setText(alarm.getAlarmTimeString());


        TextView alarmDaysView = (TextView)  convertView
                .findViewById(R.id.textView_alarm_days);
        alarmDaysView.setText(alarm.getRepeatDaysString());


        return  convertView;
    }


    protected void callMathAlarmScheduleService() {

        Log.i("启动服务", "正在启动闹钟服务接收器");
        Intent mathAlarmServiceIntent = new Intent(AppApplication.getShareInstance(), AlarmServiceBroadcastReciever.class);
        AppApplication.getShareInstance().sendBroadcast(mathAlarmServiceIntent, null);

    }
}
